package com.example.mymavendemo.common.exception;

import com.example.mymavendemo.common.lang.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    //插入不合法数据
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value=IllegalArgumentException.class)
    public <T> Result<T> handle(IllegalArgumentException e){
        log.error("Assert异常：------------{}",e.getMessage());
        return Result.fail(e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    public <T> Result<T> handler(RuntimeException e){
        log.error("运行时异常：-------------{}",e.getMessage());
        return Result.fail(e.getMessage());
    }
}
